package com.zhika.zishi.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.interfaces.TitleInterface;

/**
 * Created by Michael on 2015/4/7.
 */
public class TitleView extends RelativeLayout implements View.OnClickListener{
    
    private Button leftButton;
    
    private TableRow middleRow;
    
    private Button rightButton;
    
    private TextView middleText;
    
    private ImageView middleImageView;
    
    private LayoutInflater layoutInflater;
    
    private Context context;

    //主布局
    private LinearLayout titleMainLayout;
    
    //处理按钮点击事件的接口
    private TitleInterface titleInterface;
    
    public TitleView(Context context){
        this(context,null);
    }
    
    public TitleView(Context context, AttributeSet attributeSet){
        this(context,attributeSet,0);
    }
    
    public TitleView(Context context, AttributeSet attributeSet, int defStyle){
        super(context,attributeSet,defStyle);
        
        layoutInflater = LayoutInflater.from(context);
        
        this.context = context;
        
        initView();
        
    }
    
    private void initView(){
        View view = layoutInflater.inflate(R.layout.title_layout,this);
        leftButton = (Button) view.findViewById(R.id.title_left_button);
        leftButton.setOnClickListener(this);
        middleRow = (TableRow) view.findViewById(R.id.title_middle_row);
        middleRow.setOnClickListener(this);
        rightButton = (Button) view.findViewById(R.id.title_right_button);
        rightButton.setOnClickListener(this);
        
        middleImageView = (ImageView) view.findViewById(R.id.title_middle_image_view);
        middleText = (TextView) view.findViewById(R.id.title_middle_text);
        titleMainLayout = (LinearLayout) view.findViewById(R.id.title_main_layout);
    }

    /**
     * 设置点击事件的接口
     * @param titleInterface
     */
    public void setTitleViewInterface(TitleInterface titleInterface){
        this.titleInterface = titleInterface;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_left_button:
                titleInterface.leftTitleListener();
                break;
            case R.id.title_middle_row:
                titleInterface.middleTitleListener();
                break;
            case R.id.title_right_button:
                titleInterface.rightTitleListener();
                break;
            default:
                break;
        }
    }

    /**
     * 设置左边按钮的文字信息，并把背景设置为透明
     * @param text
     */
    public void setLeftButtonText(String text){
        leftButton.setBackgroundColor(getResources().getColor(R.color.color_transparent));
        leftButton.setText(text);
    }

    /**
     * 设置左边按钮的背景图片，并把取消文字信息
     * @param id 对应的drawable下面的图片
     */
    public void setLeftButtonImage(int id){
        leftButton.setText("");
        leftButton.setBackground(getResources().getDrawable(id));
    }

    /**
     * 设置中间标题的内容
     * @param text
     */
    public void setMiddleRowText(String text){
        middleText.setText(text);
    }

    /**
     * 显示标题栏中间的图片
     */
    public void showMiddleImageView(){
        middleImageView.setVisibility(VISIBLE);
    }

    /**
     * 隐藏标题栏中间的图片
     */
    public void hideMiddleImageView(){
        middleImageView.setVisibility(GONE);
    }

    /**
     * 修改标题栏中间的图片
     * @param id
     */
    public void setMiddleImageView(int id){
        middleImageView.setImageResource(id);
    }

    /**
     * 设置右边按钮的文字信息，并把背景设置为透明
     * @param text
     */
    public void setRightButtonText(String text){
        rightButton.setBackgroundColor(getResources().getColor(R.color.color_transparent));
        rightButton.setText(text);
    }

    /**
     * 设置右边按钮的背景图片，并把取消文字信息
     * @param id 对应的drawable下面的图片
     */
    public void setRightButtonImage(int id){
        rightButton.setText("");
        rightButton.setBackground(getResources().getDrawable(id));
    }

    /**
     * 设置主布局的背景颜色以及透明度
     * @param colorId 此值的前两位代表透明度，后六位代表颜色
     */
    public void setBackgroundAlpha(int colorId){
        titleMainLayout.setBackgroundColor(getResources().getColor(colorId));
    }

    /**
     * 隐藏右边的按钮
     */
    public void hideRightButton(){
        rightButton.setVisibility(GONE);
    }

    /**
     * 显示右边的按钮
     */
    public void showRightButton(){
        rightButton.setVisibility(VISIBLE);
    }

    /**
     * 隐藏中间所有的内容
     */
    public void hideMiddle(){
        middleRow.setVisibility(GONE);
    }
}
